package cae.gui.conf;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.netbeans.validation.api.Problem;
import org.netbeans.validation.api.Severity;
import org.netbeans.validation.api.ui.ValidationPanel;

import cae.gui.util.CaeImage;
import cae.gui.util.CaeJFrame;

import com.l2fprod.common.swing.JButtonBar;
import com.l2fprod.common.swing.plaf.blue.BlueishButtonBarUI;

public class CaeConfiguration extends CaeJFrame 
{
	private static final long serialVersionUID = 1L;
	private Component currentComponent;
	private JPanel gPanel;
	private GridBagConstraints gc;
	private JButton aceptar, aplicar, cancelar;

	public CaeConfiguration()
	{
		super("Configuracion... ");
		
		final ValidationPanel pnl = new ValidationPanel();
		getContentPane().add(pnl);
		
		pnl.addChangeListener(new ChangeListener() {

	      public void stateChanged(ChangeEvent e) {
	        Problem p = pnl.getProblem();
	        boolean enable = p == null ? true : p.severity() != Severity.FATAL;
	        System.out.println("E: " + enable);
	        
	        //setError(!enable);
	        aplicar.setEnabled(enable);
	        aceptar.setEnabled(enable);
	      }
	    });
		
		gPanel = new JPanel();
		pnl.setInnerComponent(gPanel);
		gPanel.setLayout( new GridBagLayout() );
		gc = new GridBagConstraints();
		gc.insets = new Insets(2,2,2,2);
		gc.fill = GridBagConstraints.BOTH;
				
		// BARRA
		JButtonBar toolbar = new JButtonBar(JButtonBar.VERTICAL);
		toolbar.setUI(new BlueishButtonBarUI());
		gc.gridx = 0;
		gc.gridy = 0;
		gc.gridheight = 2;
		gPanel.add(toolbar, gc);
		
		// OPCIONES DE LA BARRA
		ButtonGroup group = new ButtonGroup();
		
		//final ValidationGroup valGruop = ValidationGroup.create();
		//valGruop.add(arg0)
		addButton("Conexion", CaeImage.getImageIcon("img/net.png"), addTitle("Conexion", new CaeConexion(pnl)), toolbar, group);
		addButton("Sincronizacion", CaeImage.getImageIcon("img/net.png"), addTitle("Sincronizacion", new CaeSincronizacion(pnl)), toolbar, group);
		
		// BOTONES INFERIORES
		gc.weightx = 0;
		gc.weighty = 0;
		gc.gridwidth = 1;
		gc.gridheight = 1;
		
		gc.gridx = 1;
		gc.gridy = 1;
		gc.weightx = 100;
		gPanel.add(new JPanel(), gc);
		
		
		aceptar = new JButton("Aceptar");
		gc.gridx = 2;
		gc.gridy = 1;
		gc.weightx = 0;
		gPanel.add(aceptar, gc);
		
		cancelar = new JButton("Cancelar");
		gc.gridx = 3;
		gc.gridy = 1;
		gPanel.add(cancelar, gc);
		
		aplicar = new JButton("Aplicar");
		gc.gridx = 4;
		gc.gridy = 1;
		gPanel.add(aplicar, gc);
		
		// CONF DE APERTURA
		setSize(new Dimension(640, 480));
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenSize.width - 640)/2, (screenSize.height - 480)/2);
		
	}
	
	private Component addTitle(String title, Component panelChild)
	{
		BorderLayout layout = new BorderLayout();
		layout.setVgap(4);
		JPanel panelComponent = new JPanel(layout);
		gPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		
		// TITULO
		JLabel top = new JLabel(title);
		top.setFont(top.getFont().deriveFont(1));
		top.setOpaque(true);
		top.setBackground(gPanel.getBackground().brighter());
		top.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		panelComponent.add(BorderLayout.NORTH, top);

		panelComponent.add(panelChild, BorderLayout.CENTER);
		return panelComponent;
	}
	
	private void show(Component component) {
		if (currentComponent != null)
			gPanel.remove(currentComponent);

		currentComponent = component;
		
		gc.gridx = 1;
		gc.gridy = 0;
		gc.weightx = 100;
		gc.weighty = 100;
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gPanel.add(currentComponent, gc);
		gPanel.validate();
		gPanel.repaint();
	}

	private void addButton(String title, ImageIcon icon, Component component, JButtonBar bar, ButtonGroup group)
	{
		Action action = new TabAction(this, title, icon, component, bar);

		JToggleButton button = new JToggleButton(action);
		bar.add(button);

		group.add(button);

		if (group.getSelection() == null) {
			button.setSelected(true);
			show(component);
		}
	}
	
	/*private void addLinkButton(String title, ImageIcon icon, JButtonBar bar)
	{
		JButton button = new JButton(title);
		button.setIcon(icon);
		button.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "hola");
			}
		});
		
		bar.add(button);
	}*/
	
	private class TabAction extends AbstractAction
	{
		private static final long serialVersionUID = 1L;
		CaeConfiguration panel;
		Component component;
		//JButtonBar bar;
		
		public TabAction(CaeConfiguration panel, String title, ImageIcon icon, Component component, JButtonBar bar) {
			super(title, icon);
			this.panel = panel;
			this.component = component;
			
		}

		public void actionPerformed(ActionEvent e) {
			panel.show(component);
		}
		
	}
	
	public static void main(String[] args) {
		new CaeConfiguration().setVisible(true);
		
		
	}
	
	/*private class MyChangeListener implements ChangeListener
	{
		List<ValidationPanel>
		
		public void stateChanged(ChangeEvent e)
		{
	        Problem p = conexion.getProblem();
	        boolean enable = p == null ? true : p.severity() != Severity.FATAL;
	        okButton.setEnabled(enable);
		}*/
}
